---
version: '2'

services:

{% for peer in peers %}
{% if fabric.peer_db == 'CouchDB' %}
  couchdb-{{ peer.name }}:
    container_name: couchdb-{{ peer.name }}
    image: hyperledger/fabric-couchdb:{{ thetag }}
    network_mode: bridge
    hostname: couchdb-{{ peer.name }}
{% endif %}

  {{ peer.name }}:
    container_name: {{ peer.name }}
    image: hyperledger/fabric-peer:{{ thetag }}
    network_mode: bridge
    hostname: {{ peer.name }}
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_ENDORSER_ENABLED=true
      - CORE_PEER_GOSSIP_USELEADERELECTION={{ allpeers|selectattr('org','equalto',peer.org)|list|selectattr('role','equalto','leader')|list|length|int==0 }}
      - CORE_PEER_GOSSIP_ORGLEADER={{ (peer.role == "leader") | ternary('true','false') }}
      - CORE_PEER_PROFILE_ENABLED=true
      - CORE_PEER_TLS_ENABLED={{ fabric.tls | lower }}
{% if fabric.tls %}
      - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
{% endif %}
      - CORE_PEER_ID={{ peer.name }}
      - CORE_PEER_ADDRESS={{ peer.name }}:7051
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT={{ peer.name }}:7051
      - CORE_PEER_LOCALMSPID={{ peer.org }}
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/fabric/msp
{% if fabric.peer_db == 'CouchDB' %}
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb-{{ peer.name }}:5984
{% endif %}
    volumes:
      - /var/run/:/host/var/run/
      - {{ fabricworkdir }}/run/keyfiles/{{ peer.org }}/peers/{{ peer.name }}.{{ peer.org }}:/etc/hyperledger/fabric
      - {{ fabricworkdir }}/run/keyfiles:/etc/hyperledger/allorgs
    command: peer node start

{% endfor %}